package com.lz.library.adapter

import android.util.SparseIntArray
import android.view.ViewGroup
import androidx.annotation.LayoutRes

/**
 *  author: you : 2023/4/27
 */
abstract class SimpleMultiAdapter<T : Multiple>(initData: List<T>? = null, multipleCapacity: Int = 4)
    : com.lz.library.adapter.BaseAdapter<T, SimpleViewHolder>(initData) {
    /**
     * 不同itemViewType对应的布局id
     */
    private val layoutResIds = SparseIntArray(multipleCapacity)

    /**
     * 添加type对应的布局,设置适配器前调用,可以在子类实现构造方法中先添加好
     * @param itemViewType
     * @param layoutResId
     */
    protected fun addItemLayoutType(itemViewType: Int, @LayoutRes layoutResId: Int)
        = layoutResIds.put(itemViewType, layoutResId)

    override fun getItemViewType(position: Int): Int = data[position].viewType

    override fun onCreateItem(parent: ViewGroup, viewType: Int): SimpleViewHolder {
        val layoutResId = layoutResIds[viewType]
        check(layoutResId == 0) {"You must addItemType formats viewType = $viewType"}
        return SimpleViewHolder(layoutInflater.inflate(layoutResId, parent, false))
    }
}